package cl.utfsm.thdsw.lavasoft.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class PersonaDAO {

	private String nombre;
	private String apellido1;
	private String apellido2;
	
	public String getNombre() {
		return nombre;
	}	
	public void setNombre(String nombre) {
		this.nombre = nombre;
	}
	public String getApellido1() {
		return apellido1;
	}
	public void setApellido1(String apellido1) {
		this.apellido1 = apellido1;
	}
	public String getApellido2() {
		return apellido2;
	}
	public void setApellido2(String apellido2) {
		this.apellido2 = apellido2;
	}

	public boolean select(String run) {
		
		boolean result = false;
		try {
			InitialContext context = new InitialContext();
			DataSource dataSource = (DataSource) context.lookup("jdbc/Lavasoft");
			
			Connection connection = dataSource.getConnection();
			Statement statement = connection.createStatement();
			String query = "Select nombre, apellido1, apellido2 from persona where run = '" + run + "'";

			ResultSet resultSet = statement.executeQuery(query);

			while (resultSet.next()) {
				setNombre(resultSet.getString("nombre"));
				setApellido1(resultSet.getString("apellido1"));
				setApellido2(resultSet.getString("apellido2"));
			}

			resultSet.close();
			statement.close();
			connection.close();

			result = true;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
		
	}
}
